/*
 * Copyright 2007 - The JDPF Project Team (http://www.jdpf.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdpf.core.contextmanager;

import java.util.ArrayList;
import java.util.List;

import org.jdpf.core.contextmanager.dao.ITaskHandler;
import org.jdpf.core.kernel.execution.Task;

public class TaskManager {

	private List<Task> taskList;

	private ITaskHandler handler;

	private ContextManager cm;

	private String workspace;

	public TaskManager() {
		taskList = new ArrayList<Task>(5);
	}

	public void addTaskToList(String netFile, String paramFile) {
		Task task=cm.loadTask(netFile,paramFile);
		taskList.add(task);
		//Algorithm to check if in the storing area there is a duplicate task
		List<Task> toRemove=new ArrayList<Task>(5);
		for(int i=0;i<taskList.size()-1;i++){
			for(int j=i+1;j<taskList.size();j++){
				if(taskList.get(i).getNetFile().equals(
						taskList.get(j).getNetFile())&&
						taskList.get(i).getParamFile().equals(
								taskList.get(j).getParamFile())){
					toRemove.add(taskList.get(j));
				}
			}
		}
		//This code remove the duplicate of a task from the current task list 
		//so in the task tree you can see only one task for a pair 
		//(net file, parameter file)
		for(Task t:toRemove){
			if(taskList.contains(t)){
				taskList.remove(t);
			}
		}
		cm.setCurrentTask(null);
	}

	public void addNetToList(String netFile) {
		cm.addNet(netFile);
	}

	public List<Task> getTasks(String netFile) {
		ArrayList<Task> out = new ArrayList<Task>(5);
		for (Task task : taskList) {
			if (task.getNetFile().equals(netFile)) {
				out.add(task);
			}
		}
		return out;
	}

	public Task getTask(String netFile, String paramFile) {
		for (Task task : taskList) {
			if (task.getNetFile().equals(netFile)
					&& task.getParamFile().equals(paramFile)) {
				return task;
			}
		}
		return null;
	}
	
	public List<Task> getAllTasks(){
		return taskList;
	}

	public void addTask(Task task,String project) {
		taskList.add(task);
		handler.addTask(task,project);
	}

	public void addNet(String net,String project) {
		handler.addNet(net,project);
	}

	public void removeTask(Task t,String project) {
		taskList.remove(t);
		if(project!=null)
			handler.removeTask(t,project);
	}

	public void removeNet(String net,String project) {
		handler.removeNet(net,project);
	}

	public ContextManager getContextManager() {
		return cm;
	}

	public void setContextManager(ContextManager cm) {
		this.cm = cm;
	}

	public void setTaskHandler(ITaskHandler handler) {
		this.handler = handler;
		this.handler.addTaskManger(this);
	}
	
	public void restoreWorkspace(){
		taskList.clear();
		this.handler.listAllTasks();
		this.handler.listAllNets();
	}

	public String getWorkspace() {
		return workspace;
	}

	public void setWorkspace(String workspace) {
		this.workspace = workspace;
		handler.setWorkspace(this.workspace);
	}

}
